package co.fitstart.admin.web.controller.user.satisfaction;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import co.fitstart.admin.entity.report.CourseScheduleCount;
import co.fitstart.admin.entity.search.SatisfactionSurveySearch;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SatisfactionSurveyService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.principal.Principal;

/**
 * 满意度调查问卷发送
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("satisfaction-survey-create")
@MenuSetting("menu-user")
public class SatisfactionSurveyCreateController {

    private static final Logger logger = LoggerFactory.getLogger(SatisfactionSurveyCreateController.class);

    @Autowired
    private SiteService siteService;

    @Autowired
    private PrincipalService principalService;

    @Autowired
    private SatisfactionSurveyService satisfactionSurveyService;

    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-satisfaction-survey-create-management")
    public ModelAndView index(@RequestParam(value = "siteId", required = false) Long siteId) {
        ModelAndView modelAndView = new ModelAndView("market/satisfaction-survey/satisfaction-survey");
        
        modelAndView.addObject("siteId", siteId);
        
        return modelAndView;
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-satisfaction-survey-create-management")
    public ModelAndView search(@RequestParam(value = "siteId", required = false) Long siteId,
                               @RequestParam(value = "coachId", required = false) Long coachId) {
        ModelAndView modelAndView = new ModelAndView("market/satisfaction-survey/satisfaction-survey");
        
        SatisfactionSurveySearch search = new SatisfactionSurveySearch();
        
        if(siteId != null) {
            search.setSite(siteService.load(siteId));
        }
        
        if(coachId != null) {
            search.setCoach(principalService.load(Principal.class, coachId));
        }
        
        List<CourseScheduleCount> courseRounds = satisfactionSurveyService.listSatisfactionSurveySearch(search);
        
        modelAndView.addObject("courseRounds", courseRounds);
        modelAndView.addObject("search", true);
        modelAndView.addObject("siteId", siteId);
        modelAndView.addObject("coachId", coachId);
        
        return modelAndView;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onSend(@RequestParam("courseRoundId") Long[] courseRoundIds) {
        
        Result result = new Result();
        
        try {
            satisfactionSurveyService.send(courseRoundIds);
            
            result.success().message("发送成功");
            
        } catch (Exception e) {
            
            logger.error("onSend() error: ", e);
            
            result.fail().message("发送失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
